3  Get environmental data

Data are generated in R/getDataEnv-targets.R file

3.1 Get environmental data target and show data frame structure

Flow data in envDataWB:
flow = original flow extension estimates (WB only)
flowWithTribs = original flow estimates added to the tribs (not adjusted for area)

flowByRiver = Jenn’s hindcasted flow based on tributary-specific flow estimates
flowByRiverCFS = Jenn’s hindcasted flow in CFS based on tributary-specific flow estimates

flowByRiverWB_WithTribs = flowByRiver estimates from WB added to the tribs (for scaling in flowByArea_ByRiver)

flowByArea_flowExt = flow extension estimates (flowWithTribs * propRiverArea)
flowByArea_ByRiver = flow extension estimates scaled by watershed area (flowByRiverWB_WithTribs * propRiverArea)

Code
envDataWB <- tar_read(envDataWB_target)
str(envDataWB)
tibble [32,189 × 31] (S3: tbl_df/tbl/data.frame)
 $ river                  : chr [1:32189] "wb jimmy" "wb jimmy" "wb jimmy" "wb jimmy" ...
 $ date                   : POSIXct[1:32189], format: "1997-05-14 04:00:00" "1997-05-15 04:00:00" ...
 $ temperature            : num [1:32189] 11.57 10.4 10.55 8.84 9.09 ...
 $ daily_max_temp         : num [1:32189] 11.6 12.3 11.9 10.2 11.6 ...
 $ daily_min_temp         : num [1:32189] 11.57 8.96 9.73 7.74 7.45 ...
 $ flow                   : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ dateDate               : Date[1:32189], format: "1997-05-14" "1997-05-15" ...
 $ yday                   : num [1:32189] 134 135 136 137 138 139 140 141 142 143 ...
 $ year                   : num [1:32189] 1997 1997 1997 1997 1997 ...
 $ riverOrdered           : Ord.factor w/ 4 levels "West Brook"<"WB Jimmy"<..: 2 2 2 2 2 2 2 2 2 2 ...
 $ dayLength              : num [1:32189] 51770 51896 52019 52140 52258 ...
 $ precip                 : num [1:32189] 0 1.15 0 0 2.34 ...
 $ solarRadiation         : num [1:32189] 587 562 259 434 446 ...
 $ swe                    : num [1:32189] 78.7 73.6 67.9 63 59.5 ...
 $ airTempMax             : num [1:32189] 20.1 21.2 12.7 15.1 18.9 ...
 $ airTempMin             : num [1:32189] 4.03 6.28 7.47 5.64 2.38 6.59 7.91 3.28 4.98 3.99 ...
 $ vaporPressure          : num [1:32189] 815 953 1034 911 725 ...
 $ airTempMedian          : num [1:32189] 12.1 13.7 10.1 10.4 10.6 ...
 $ ETmm                   : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ Pmm                    : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ flowByRiver_cfs        : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ flowByRiver            : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ season                 : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ start                  : Date[1:32189], format: NA NA ...
 $ end                    : Date[1:32189], format: NA NA ...
 $ flowWithTribs          : num [1:32189] NA 0.234 0.227 0.198 0.191 ...
 $ flowByRiverWB_WithTribs: num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ propRiverArea          : num [1:32189] 0.113 0.113 0.113 0.113 0.113 ...
 $ flowByArea_flowExt     : num [1:32189] NA 0.0265 0.0257 0.0224 0.0216 ...
 $ flowByArea_ByRiver     : num [1:32189] NA NA NA NA NA NA NA NA NA NA ...
 $ dummy                  : num [1:32189] 1 1 1 1 1 1 1 1 1 1 ...
Code
# Save rData file for downloading
# save(envDataWB, file = "C:/Users/bletcher/OneDrive - DOI/projects/wbBook_quarto_targets/data/outForDownload/envDataWB.RData")
# write.csv(envDataWB, file = "C:/Users/bletcher/OneDrive - DOI/projects/wbBook_quarto_targets/data/outForDownload/envDataWB.csv")

ojs_define(envDataWB_OJS = transpose(envDataWB))

3.2 Plot flow over time for the West Brook by year

Flow estimates from flow extension model for the West Brook

Code
# ggplot(envDataWB %>% filter(riverOrdered == "West Brook"), aes(yday, flow)) +
#   geom_point(size = 0.1) +
#   #geom_point(size = 0.1, color = "blue", aes(yday, flowByRiver)) +
#   #geom_line() +
#   scale_x_continuous("Day of year") +
#   scale_y_continuous("Stream flow (m^3/s)") +
#   facet_wrap(~year(date))

table(envDataWB$river, is.na(envDataWB$flow))
             
              FALSE TRUE
  wb jimmy        0 7717
  wb mitchell     0 7717
  wb obear        0 7717
  west brook   9011   27
Code
ggplot(envDataWB, aes(yday, flow)) +
  geom_point(size = 0.1) +
  #geom_point(size = 0.1, color = "blue", aes(yday, flowByRiver)) +
  #geom_line() +
  scale_x_continuous("Day of year") +
  scale_y_continuous("Stream flow (m^3/s)") +
  facet_wrap(~year(date))
Figure 3.1: Stream flow (m3/s) for the West Brook
Code
table(envDataWB$river, is.na(envDataWB$flowByArea_ByRiver))
             
              FALSE TRUE
  wb jimmy     7572  145
  wb mitchell  7572  145
  wb obear     7572  145
  west brook   8760  278
Code
ggplot(envDataWB, aes(yday, flowByArea_ByRiver, color = river)) +
  geom_point(size = 0.1) +
  #geom_point(size = 0.1, color = "blue", aes(yday, flowByRiver)) +
  #geom_line() +
  scale_x_continuous("Day of year") +
  scale_y_continuous("Stream flow (m^3/s) scaled by watershed area") +
  facet_wrap(~year(date))
Figure 3.2: Stream flow (m3/s) scaled by watershed area
Code
table(envDataWB$river, is.na(envDataWB$flowByRiver))
             
              FALSE TRUE
  wb jimmy     7572  145
  wb mitchell  7572  145
  wb obear     7572  145
  west brook   8760  278
Code
ggplot(envDataWB, aes(yday, flowByRiver, color = river)) +
  geom_point(size = 0.1) +
  #geom_point(size = 0.1, color = "blue", aes(yday, flowByRiver)) +
  #geom_line() +
  scale_x_continuous("Day of year") +
  scale_y_continuous("Stream flow (m^3/s) hindcasted by river-specific flows") +
  facet_wrap(~year(date))
Figure 3.3: Hindcasted stream flow (m3/s) for the West Brook and tributaries
Code
ggplot(envDataWB %>% filter(riverOrdered == "West Brook", year %in% (1998:2021)), aes(flow, flowByRiver, color = yday)) +
  geom_point(size = 0.1) +
  geom_abline(slope = 1) +
  scale_y_continuous("Stream flow (m^3/s) by river") +
  scale_x_continuous("Stream flow (m^3/s) from flow extension") +
  facet_wrap(~year(date))

ggplot(envDataWB %>% filter(year %in% (1998:2021)), aes(flow, flowByRiver, color = river)) +
  geom_point(size = 0.1) +
  geom_abline(slope = 1) +
  scale_y_continuous("Stream flow (m^3/s) by river") +
  scale_x_continuous("Stream flow (m^3/s) from flow extension") +
  facet_wrap(~river)
Figure 3.4: Stream flow (m3/s) for the West Brook with Jenn’s model predictions
Figure 3.5: Stream flow (m3/s) for the West Brook with Jenn’s model predictions
Code
ggplot(envDataWB %>% filter(year %in% (1998:2021)), aes(flowByArea_ByRiver, flowByRiver, color = river)) +
  geom_point(size = 0.1) +
  geom_abline(slope = 1) +
  scale_x_continuous("Stream flow (m^3/s) by area") +
  scale_y_continuous("Stream flow (m^3/s) by river") +
  facet_wrap(~year(date))

ggplot(envDataWB %>% filter(year %in% (1998:2021)), aes(flowByArea_ByRiver, flowByRiver, color = river)) +
  geom_point(size = 0.1) +
  #geom_abline(slope = 1) +
  scale_x_continuous("Stream flow (m^3/s) by area") +
  scale_y_continuous("Stream flow (m^3/s) by river") +
  facet_wrap(~river)
Figure 3.6: Stream flow (m3/s) for the West Brook with Jenn’s model predictions
Figure 3.7: Stream flow (m3/s) for the West Brook with Jenn’s model predictions
Code
viewof rangeYear = Inputs.range([1997, 2022], {
  label: "Which year?",
  value: 2002,
  step: 1
})

envDataWB_OJSplot = envDataWB_OJS.filter(d => d.year === rangeYear)
Code
Plot.plot({
    width: width,
    height: 350,
    inset: 10,
    color: {
      scheme: "greys"
    },
    x: { label: "Stream flow (m3/s)" },
    y: { label: "Stream flow (m3/s) from Jenn's model" },
    marks: [
      Plot.frame(),
      Plot.dot(envDataWB_OJSplot, {
        x: "flow",
        y: "flowByRiver"
      })
    ],
    facet: {
      data: envDataWB_OJSplot,
      x: "riverOrdered"
    }
  });
Code
Plot.plot({
    width: width,
    height: 350,
    inset: 10,
    color: {
      type: "categorical"
    },
    x: { label: "Day of year" },
    y: { label: "Stream flow (m3/s)" },
    marks: [
      Plot.frame(),
      Plot.dot(envDataWB_OJSplot, {
        x: "dateDate",
        y: "flowByRiver"
      })
    ],
    marks: [
      Plot.frame(),
      Plot.dot(envDataWB_OJSplot, {
        x: "dateDate",
        y: "flow"
      })
    ],
    facet: {
      data: envDataWB_OJSplot,
      x: "riverOrdered"
    }
  });

Test: referring to Figure fig-envFlow.

3.3 Plot temperature over time for each tributary

Code
ggplot(envDataWB, aes(date, temperature)) +
  geom_point(size = 0.2) +
  scale_x_continuous("Date") +
  scale_y_continuous("Stream temperature (C)") +
  facet_wrap(~river)
Figure 3.8: Stream temperature (C) by river

3.4 Plot air and water temperature over time for each tributary

Code
getTempYear <- function(d, yearIn){
  d %>% filter(year == yearIn)
}

tempYear <- getTempYear(envDataWB, 2002)
#write.csv(tempYear, "./data/tempYear.csv")
ojs_define(tempYearOJS0 = tempYear)

ggplot(tempYear, aes(date, temperature)) +
  geom_point(size = 0.2) +
  geom_point(aes(date, airTempMedian), color = "blue", size = 0.2) +
  scale_x_continuous("Date") +
  scale_y_continuous("Stream temperature (C)") +
  facet_wrap(~river)

ggplot(tempYear, aes(airTempMedian, temperature, color = yday)) +
  geom_point(size = 0.2) +
  scale_x_continuous("Air Temperature") +
  scale_y_continuous("Stream temperature (C)") +
  facet_wrap(~river)
Figure 3.9: Stream and air temperature (C) by river
Figure 3.10: Stream and air temperature (C) by river

Will move this to its own chapter

Code
tempYearOJS = transpose(tempYearOJS0)

//tempYearOJS0
//tempYearOJS
dateChunks
Code
dateChunks = {
  let daysCount = rangeWindowWidth;
  let days = [
    {
      daysCount0: 0,
      start: tempYearOJS[0].dateDate,
      end: tempYearOJS[rangeWindowWidth].dateDate,
      daysCount: rangeWindowWidth,
      data: tempYearOJS.filter(
        (d) => d.yday >= 0 && d.yday <= 0 + rangeWindowWidth
      )
    }
  ];

  do {
    if (daysCount < 365) {
      // dont push if the whole year is seleceted, array will be empty
      days.push({
        daysCount0: daysCount,
        start: tempYearOJS[daysCount].dateDate,
        end: tempYearOJS[daysCount + rangeWindowWidth].dateDate,
        daysCount: daysCount,
        data: tempYearOJS.filter(
          (d) =>
            d.yday >= daysCount + 1 && d.yday <= daysCount + rangeWindowWidth
        )
      });
    }
    daysCount = daysCount + rangeWindowWidth;
  } while (daysCount <= 365);
  return days;
}

Use the slider below to set the length (# of days) of each data chunk.
Number of data chunks:
Start date =
end date =

Code
viewof rangeWindowWidth = Inputs.range([1, 365 - 0], {
  label: "Data chunk length",
  value: 14,
  step: 1
})

Of the data chunks defined above which one should we graph. 0 is the first one and you won’t be able to go past the last one (range updates automatically) but you might get as few as one observation in the last chunk.

Code
viewof rangeWhichWindow = Inputs.range([0, dateChunks.length - 1], {
  label: "Which data chunk?",
  step: 1,
  value: 0
})
Code
Plot.plot({
  width: width,
  height: 350,
  inset: 10,
  color: {
    scheme: "greys"
  },
  x: { label: "Air temperature (C)" },
  y: { label: "Water temperature (C)" },
  marks: [
    Plot.frame(),
    Plot.dot(dateChunks[rangeWhichWindow].data, {
      x: "airTempMedian",
      y: "temperature",
      stroke: "yday"
    })//,
 //   Plot.linearRegressionY(dateChunks[rangeWhichWindow].data, {
//      x: "airTempMedian",
 //     y: "temperature"
 //   })
  ],
  facet: {
    data: dateChunks[rangeWhichWindow].data,
    x: "river"
  }
})

=======================================================
some integration alternatives - most are not so slick and are independent from the ojs code blocks above
Seems like it is best to devleop in observable and copy/paste code blocks in here.

import chunk from ‘Playground’

Code
import { plotChunk } from "@bletcher/Playground"
plotChunk

iframe in html block

runtime with javascript in html block

Credit: Playground by Ben Letcher